package com.util;

import java.io.IOException;  
import java.net.URI;  
import java.util.ArrayList;
import java.util.List;  

import org.apache.http.HttpEntity;  
import org.apache.http.HttpResponse;  
import org.apache.http.ParseException;  
import org.apache.http.client.entity.UrlEncodedFormEntity;  
import org.apache.http.client.methods.HttpGet;  
import org.apache.http.client.methods.HttpPost;  
import org.apache.http.client.methods.HttpRequestBase;  
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.concurrent.FutureCallback;  
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;  
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;  
import org.apache.http.message.BasicNameValuePair;  
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;  
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  
import org.springframework.beans.factory.annotation.Autowired;
  
/** 
 *  
 * http client 业务逻辑处理类 
 * */  
public class HttpClientService {  
	
    private static Logger LOG = LoggerFactory  
            .getLogger(HttpClientService.class);  
  

	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected void exeAsyncReq(String baseUrl, String methodName,  
            List<BasicNameValuePair> urlParams,  
            String postBody, FutureCallback callback)
            throws Exception {  
  
        if (baseUrl == null) {  
            LOG.warn("we don't have base url, check config");  
            throw new Exception("missing base url");  
        }  
  
        HttpRequestBase httpMethod;  
        CloseableHttpAsyncClient hc = null;  
  
        try {  
//            hc = HttpClientFactory.getInstance().getHttpAsyncClientPool()  
//                    .getAsyncHttpClient();  
        	hc = HttpAsyncClient.getInstance().getAsyncHttpClient();
        	//子类重写？
            hc.start();
  
            HttpClientContext localContext = HttpClientContext.create();  
            BasicCookieStore cookieStore = new BasicCookieStore();  

            if (HttpPost.METHOD_NAME.equals(methodName)) {
                httpMethod = new HttpPost(baseUrl);  
  
                if (null != postBody) {  
                    //LOG.debug("exeAsyncReq post postBody={}", postBody);  
//                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(  
//                            postBody, "UTF-8");

                    @SuppressWarnings("deprecation")
					HttpEntity entity = new StringEntity(postBody, HTTP.UTF_8); 
                    ((HttpPost) httpMethod).setEntity(entity);  
                }  
  
                if (null != urlParams) {  
  
                    String getUrl = EntityUtils  
                            .toString(new UrlEncodedFormEntity(urlParams));  
  
                    httpMethod.setURI(new URI(httpMethod.getURI().toString()  
                            + "?" + getUrl));  
                }  
  
            } else if (HttpGet.METHOD_NAME.equals(methodName)){  
  
                httpMethod = new HttpGet(baseUrl);  
  
                if (null != urlParams) {  
  
                    String getUrl = EntityUtils  
                            .toString(new UrlEncodedFormEntity(urlParams));  
  
                    httpMethod.setURI(new URI(httpMethod.getURI().toString()  
                            + "?" + getUrl));  
                }  
            }
            else {
            	httpMethod = null;
            	
            	LOG.warn("httpMethod is null");
            }
  
            LOG.info("exeAsyncReq getparams:" + httpMethod.getURI());  
  
            localContext.setAttribute(HttpClientContext.COOKIE_STORE,  
                    cookieStore);  
  
            hc.execute(httpMethod, localContext, callback);

  
        } catch (Exception e) {  
            LOG.error(e.getMessage(),e);
        }  
  
    }
  
    public static String getHttpContent(HttpResponse response) {
  
        HttpEntity entity = response.getEntity();  
        String body = null;  
  
        if (entity == null) {  
            return null;  
        }  
  
        try {  
  
            body = EntityUtils.toString(entity, "utf-8");  
  
        } catch (ParseException e) {  
  
            LOG.warn("the response's content inputstream is corrupt", e);  
        } catch (IOException e) {  
  
            LOG.warn("the response's content inputstream is corrupt", e);
        }  
        return body;  
    }  
}